package com.alennus.blog.controller;

import com.alennus.blog.domain.Menu;
import com.alennus.blog.domain.Role;
import com.alennus.blog.response.WebApiResponse;
import com.alennus.blog.service.RoleService;
import com.alennus.blog.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by pactera on 2018/5/23.
 */
@RestController
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private RoleService roleService;



    @GetMapping()
    public WebApiResponse<Page<Role>> roles(Role role,Pageable pageable){
        return WebApiResponse.success(roleService.findRoles(role,pageable));
    }

    @PostMapping("/save")
    public WebApiResponse<Role> save(@RequestBody @Valid Role role){
        if(StringUtils.isEmpty(role.getId())){
            role.setCreatedDate(DateUtils.getCurrentDatetime());
            role.setUpdateDate(DateUtils.getCurrentDatetime());
        }else{
            role.setUpdateDate(DateUtils.getCurrentDatetime());
        }
        return WebApiResponse.success(roleService.saveOrUpdate(role));
    }


    @PostMapping("/delete")
    public WebApiResponse delete(@RequestBody List<Role> roles){
        roleService.delete(roles);
        return WebApiResponse.success();
    }

}
